package com.xnx3.wangmarket.admin.service.impl;

import com.xnx3.j2ee.dao.SqlDAO;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.admin.entity.*;
import com.xnx3.wangmarket.admin.service.CompanyInfoService;
import com.xnx3.wangmarket.admin.vo.CompanyInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.xnx3.j2ee.shiro.ShiroFunc.getUserId;

@Service("companyInfoService")
public class CompanyInfoImpl implements CompanyInfoService {
    @Resource
    private SqlDAO sqlDAO;

    @Override
    public List<CompanyInfo> queryCompanyInfo(int userid) {
        return sqlDAO.findBySqlQuery("select * from Company_Info where user_id=" + userid, CompanyInfo.class);
    }

    @Override
    public void updateCompanyInfo(CompanyInfo companyInfo, int userid, HttpServletRequest request) {
        sqlDAO.save(companyInfo);
    }

    @Override
    public void delCompanyInfo(CompanyInfo companyInfo) {
        sqlDAO.delete(companyInfo);
    }

    @Override
    public List<Product> queryProductInfo(int userid) {
        return sqlDAO.findBySqlQuery("select * from product where user_id=" + userid + "order by timestamps desc limit 1", Product.class);
    }

    @Override
    public List<News> queryNewList(int userid, Integer id) {
        return sqlDAO.findBySqlQuery("SELECT * FROM news where userid = " + userid + " and cid=" + id, News.class);
    }

    @Override
    public List<Product> queryProduct(int userid) {
        return sqlDAO.findBySqlQuery("SELECT  *  FROM   product  where user_id =" + userid + " ORDER BY  TIMESTAMPs  desc  LIMIT   4", Product.class);
    }

    @Override
    public List<News> queryNews(int userid, int cid) {
        return sqlDAO.findBySqlQuery("SELECT * FROM `news` where userid = " + userid + " and cid=" + cid + " and type=1 ORDER BY addtime DESC LIMIT 5", News.class);
    }

    @Override
    public List<News> queryAllNews(int userid, int cid) {
        return sqlDAO.findBySqlQuery("SELECT * FROM `news` where userid = " + userid + " and cid = " + cid + " and type=1 ORDER BY addtime DESC LIMIT 5", News.class);
    }

    @Override
    public List<NewsData> queryNewsData(int id) {
        return sqlDAO.findBySqlQuery("select * from news_data where id =" + id, NewsData.class);
    }

    @Override
    public List<SiteColumn> queryNewsOneColumn(int userid) {
        return sqlDAO.findBySqlQuery("SELECT * FROM `site_column` where userid=" + userid + " and type=1", SiteColumn.class);
    }

    @Override
    public List<SiteColumn> queryNewsTwoColumn(int parentid, int userid) {
        return sqlDAO.findBySqlQuery("select * from site_column where parentid = " + parentid + " and userid = " + userid, SiteColumn.class);
    }

    @Override
    public List<EmpiricalConclusion> queryEmpirical(int userid) {
        return sqlDAO.findBySqlQuery("SELECT  *  FROM   empirical_conclusion  where user_id =" + userid + " ORDER BY  TIMESTAMPs  desc LIMIT   4", EmpiricalConclusion.class);
    }

    @Override
    public List<Product> twoSelProduct(int siteColumnId, int userid) {
        return sqlDAO.findBySqlQuery("SELECT * FROM `product` where site_column_id = " + siteColumnId + " and user_id = " + userid, Product.class);
    }

    @Override
    public List<EmpiricalConclusion> twoSelEmpircal(int siteColumnId, int userid) {
        return sqlDAO.findBySqlQuery("SELECT * FROM `empirical_conclusion` where site_column_id = " + siteColumnId + " and user_id = " + userid, EmpiricalConclusion.class);
    }

    @Override
    public List<Product> allProduct() {
        return sqlDAO.findBySqlQuery("SELECT  *  FROM   product  ORDER BY  TIMESTAMPs  desc  LIMIT   8", Product.class);
    }

    @Override
    public List<Site> querySite(int userId) {
        return sqlDAO.findBySqlQuery("select * from site where userId = " + userId, Site.class);
    }

    @Override
    public List<Site> queryCountNum() {
        return sqlDAO.findBySqlQuery("select * from site ORDER BY  count_Num  desc  LIMIT   5", Site.class);
    }

    @Override
    public void updateCountNum(int userId, int count) {
        sqlDAO.executeSql("update site  set count_Num=" + count + " where userId=" + userId);
    }

    @Override
    public List<Site> queryNum(int userId) {
        return sqlDAO.findBySqlQuery("select * from site where userid="+userId, Site.class);
    }
}
